package net.esj.background.spring;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.esj.background.support.AccessDepend;
import net.esj.background.support.AccessDependFinder;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringAccessDependFinder extends AccessDependFinder implements ApplicationContextAware,InitializingBean{

	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Map<String,AccessDepend> map = applicationContext.getBeansOfType(AccessDepend.class);
		
		
		for(AccessDepend acc : map.values()){
			if(ACCESS_DEPENDS.containsKey(acc.getGenericClass())){
				ACCESS_DEPENDS.get(acc.getGenericClass()).add(acc);
			}else{
				Set<AccessDepend> set = new HashSet<AccessDepend>();
				set.add(acc);
				ACCESS_DEPENDS.put(acc.getGenericClass(), set);
			}
		}
	}

}
